#include "sysdllFun.h"
#include "lcstr.h"

void * GetMoudleAddress(wchar_t * dllname)
//void * GetKernel32Address()
{
	void * ptrret = 0;
	_PEB_LDR_DATA * dll;
	PPEB peb;
	__asm
	{
		mov eax,DWORD ptr fs:0x30;
		mov DWORD ptr[peb],eax;
	}
	dll = (_PEB_LDR_DATA *)peb->DllList;
	_LDR_MODULE * moudle = (_LDR_MODULE * )dll->InLoadOrderModuleList.Flink;
	for(;moudle->BaseAddress;)
	{
		if(!lcstrcmpW(moudle->BaseDllName.Buffer,dllname))
		{
			ptrret = moudle->BaseAddress;
			break;
		}
		moudle =(_LDR_MODULE * ) moudle->InLoadOrderModuleList.Flink;
	}
	return ptrret;
}
void * GetntAddress()
{
	wchar_t dllname[100];
	dllname[0] = 'n';
	dllname[1] = 't';
	dllname[2] = 'd';
	dllname[3] = 'l';
	dllname[4] = 'l';
	dllname[5] = '.';
	dllname[6] = 'd';
	dllname[7] = 'l';
	dllname[8] = 'l';
	dllname[9] = 0;
	return GetMoudleAddress(dllname);
}
void * GetKernel32Address()
{
	wchar_t dllname[100];
	dllname[0] = 'K';
	dllname[1] = 'e';
	dllname[2] = 'r';
	dllname[3] = 'n';
	dllname[4] = 'e';
	dllname[5] = 'l';
	dllname[6] = '3';
	dllname[7] = '2';
	dllname[8] = '.';
	dllname[9] = 'd';
	dllname[10] = 'l';
	dllname[11] = 'l';
	dllname[12] = 0;
	return GetMoudleAddress(dllname);
}
void * GetLoadLibAddress()
{
	char szFunName[512];
	DWORD * NAME = (DWORD *)&szFunName[0];
	NAME[0] = 0x64616f4c;
	NAME[1] = 0x7262694c;
	NAME[2] = 0x41797261;
	NAME[3] = 0;
	IMAGE_DOS_HEADER * pKernel32 = (IMAGE_DOS_HEADER*)GetKernel32Address();
	char * pKernel32_2 = (char *)pKernel32;
	pKernel32_2+=pKernel32->e_lfanew;
	IMAGE_NT_HEADERS * pKernel32_4 = (IMAGE_NT_HEADERS * )pKernel32_2;
	IMAGE_EXPORT_DIRECTORY * pexport = (IMAGE_EXPORT_DIRECTORY *)(((unsigned int)pKernel32) + pKernel32_4->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress);
	char * * pdatafunname = (char * *)(((unsigned int)pKernel32) + pexport->AddressOfNames);
	unsigned short int * pdatafunOder = (unsigned short int *)(((unsigned int)pKernel32) + pexport->AddressOfNameOrdinals);
	unsigned int * pdatafunaddress = (unsigned int *)(((unsigned int)pKernel32) + pexport->AddressOfFunctions);
	for(int i = 0;i<pexport->NumberOfNames;i++)
	{
		char * data= (char *)(pdatafunname[i]+((unsigned int)pKernel32));
		if (!lcstrcmpA(data,szFunName))
		{
			for (int j= 0;j<pexport->NumberOfFunctions;j++)
			{
				if (pdatafunOder[j] == i)
				{
					unsigned int dwdata =	pdatafunaddress[j];
		//			unsigned short int dwdata = pdatafunOder[i];
					return (void *)(pdatafunaddress[j] + ((unsigned int)pKernel32));
				}
			}
		}
	}
//	LoadLibrary("KERNEL32.DLL");
	return 0;
}


void * GetProcAddressddress()
{
	char szFunName[512];
	DWORD * NAME = (DWORD *)&szFunName[0];
	NAME[0] = 0x50746547;
	NAME[1] = 0x41636f72;
	NAME[2] = 0x65726464;
	NAME[3] = 0x00007373;
	IMAGE_DOS_HEADER * pKernel32 = (IMAGE_DOS_HEADER*)GetKernel32Address();
	char * pKernel32_2 = (char *)pKernel32;
	pKernel32_2+=pKernel32->e_lfanew;
	IMAGE_NT_HEADERS * pKernel32_4 = (IMAGE_NT_HEADERS * )pKernel32_2;
	IMAGE_EXPORT_DIRECTORY * pexport = (IMAGE_EXPORT_DIRECTORY *)(((unsigned int)pKernel32) + pKernel32_4->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress);
	char * * pdatafunname = (char * *)(((unsigned int)pKernel32) + pexport->AddressOfNames);
	unsigned short int * pdatafunOder = (unsigned short int *)(((unsigned int)pKernel32) + pexport->AddressOfNameOrdinals);
	unsigned int * pdatafunaddress = (unsigned int *)(((unsigned int)pKernel32) + pexport->AddressOfFunctions);
	for(int i = 0;i<pexport->NumberOfNames;i++)
	{
		char * data= (char *)(pdatafunname[i]+((unsigned int)pKernel32));
		if (!lcstrcmpA(data,szFunName))
		{
			for (int j= 0;j<pexport->NumberOfFunctions;j++)
			{
				if (pdatafunOder[j] == i)
				{
					unsigned int dwdata =	pdatafunaddress[j];
					//			unsigned short int dwdata = pdatafunOder[i];
					return (void *)(pdatafunaddress[j] + ((unsigned int)pKernel32));
				}
			}
		}
	}
	//	LoadLibrary("KERNEL32.DLL");
	return 0;
}
void * GetMessageBoxAddress()
{
	char dllname[100];
	dllname[0] = 'u';
	dllname[1] = 's';
	dllname[2] = 'e';
	dllname[3] = 'r';
	dllname[4] = '3';
	dllname[5] = '2';
	dllname[6] = '.';
	dllname[7] = 'd';
	dllname[8] = 'l';
	dllname[9] = 'l';
	dllname[10] = 0;
	
	char funname[100];
	funname[0] = 'M';
	funname[1] = 'e';
	funname[2] = 's';
	funname[3] = 's';
	funname[4] = 'a';
	funname[5] = 'g';
	funname[6] = 'e';
	funname[7] = 'B';
	funname[8] = 'o';
	funname[9] = 'x';
	funname[10] = 'A';
	funname[11] = 0;
	FunLoadLibraryA fun= (FunLoadLibraryA)GetLoadLibAddress();
	FunGetProcAddress fun2= (FunGetProcAddress)GetProcAddressddress();
	void * fun3 = (void *)fun2(fun(dllname),funname);
	return fun3;
}



void * GetLocalAlloc()
{
	char dllname[100];
	dllname[0] = 'K';
	dllname[1] = 'e';
	dllname[2] = 'r';
	dllname[3] = 'n';
	dllname[4] = 'e';
	dllname[5] = 'l';
	dllname[6] = '3';
	dllname[7] = '2';
	dllname[8] = '.';
	dllname[9] = 'd';
	dllname[10] = 'l';
	dllname[11] = 'l';
	dllname[12] = 0;
	
	char funname[100];
	funname[0] = 'L';
	funname[1] = 'o';
	funname[2] = 'c';
	funname[3] = 'a';
	funname[4] = 'l';
	funname[5] = 'A';
	funname[6] = 'l';
	funname[7] = 'l';
	funname[8] = 'o';
	funname[9] = 'c';
	funname[10] = 0;
	FunLoadLibraryA fun= (FunLoadLibraryA)GetLoadLibAddress();
	FunGetProcAddress fun2= (FunGetProcAddress)GetProcAddressddress();
	void * fun3 = (void *)fun2(fun(dllname),funname);
	return fun3;
}
void * GetLocalFree()
{
	char dllname[100];
	dllname[0] = 'K';
	dllname[1] = 'e';
	dllname[2] = 'r';
	dllname[3] = 'n';
	dllname[4] = 'e';
	dllname[5] = 'l';
	dllname[6] = '3';
	dllname[7] = '2';
	dllname[8] = '.';
	dllname[9] = 'd';
	dllname[10] = 'l';
	dllname[11] = 'l';
	dllname[12] = 0;
	
	char funname[100];
	funname[0] = 'L';
	funname[1] = 'o';
	funname[2] = 'c';
	funname[3] = 'a';
	funname[4] = 'l';
	funname[5] = 'F';
	funname[6] = 'r';
	funname[7] = 'e';
	funname[8] = 'e';
	funname[9] = 0;
	FunLoadLibraryA fun= (FunLoadLibraryA)GetLoadLibAddress();
	FunGetProcAddress fun2= (FunGetProcAddress)GetProcAddressddress();
	void * fun3 = (void *)fun2(fun(dllname),funname);
	return fun3;	
}

void * GetGetModuleHandleA()
{
	char dllname[100];
	dllname[0] = 'K';
	dllname[1] = 'e';
	dllname[2] = 'r';
	dllname[3] = 'n';
	dllname[4] = 'e';
	dllname[5] = 'l';
	dllname[6] = '3';
	dllname[7] = '2';
	dllname[8] = '.';
	dllname[9] = 'd';
	dllname[10] = 'l';
	dllname[11] = 'l';
	dllname[12] = 0;
	
	char funname[100];
	funname[0] = 'G';
	funname[1] = 'e';
	funname[2] = 't';
	funname[3] = 'M';
	funname[4] = 'o';
	funname[5] = 'd';
	funname[6] = 'u';
	funname[7] = 'l';
	funname[8] = 'e';
	funname[9] = 'H';
	funname[10] = 'a';
	funname[11] = 'n';
	funname[12] = 'd';
	funname[13] = 'l';
	funname[14] = 'e';
	funname[15] = 'A';
	funname[16] = 0;
	FunLoadLibraryA fun= (FunLoadLibraryA)GetLoadLibAddress();
	FunGetProcAddress fun2= (FunGetProcAddress)GetProcAddressddress();
	void * fun3 = (void *)fun2(fun(dllname),funname);
	return fun3;	
}
